package cn.tedu.zx.product.webapi.ex.handler;


import cn.tedu.zx.product.webapi.web.JsonResult;
import cn.tedu.zx.product.webapi.ex.ServiceCode;
import cn.tedu.zx.product.webapi.ex.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;

@Slf4j
@RestControllerAdvice/*作用于整个项目中所有的异常处理，不需要继承*/
public class GlobalExceptionHandler {
    private String message = null;
    @ExceptionHandler
    public JsonResult handlerServiceException(ServiceException e){
        log.debug("程序中运行处出现的异常，将统一处理");
        log.warn("处理的异常信息，{}",e.getMessage());
        return JsonResult.fail(e.getServiceCode(),e.getMessage());
    }
    @ExceptionHandler
    public JsonResult handlerStringIndexOutOfBoundsException(StringIndexOutOfBoundsException e){
        log.debug("程序中运行处出现的异常，将统一处理");
        log.warn("处理的异常信息，{}",e.getMessage());
        /*JsonResult jsonResult = new JsonResult();
        jsonResult.setState(2);
        jsonResult.setMessage(e.getMessage());
        return jsonResult;*/
        return JsonResult.fail(ServiceCode.ERR_CONTACT,e.getMessage());
    }
    @ExceptionHandler
    public JsonResult handlerConstraintViolationException(ConstraintViolationException e){
        log.debug("程序中运行处出现的异常，将统一处理");
        log.warn("处理的异常信息，{}",e.getMessage());
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        for (ConstraintViolation constraintViolation :constraintViolations
             ) {
            message = constraintViolation.getMessage();
        }
        /*JsonResult jsonResult = new JsonResult();
        jsonResult.setState(3);
        jsonResult.setMessage(message);
        return jsonResult;*/
        return JsonResult.fail(ServiceCode.ERR_CONTACT,message);
    }
    @ExceptionHandler
    public JsonResult handlerBindException(BindException e){
        log.debug("程序中运行处出现的异常，将统一处理");
        log.warn("处理的异常信息，{}",e.getMessage());
        /*方法一*/
       /* message = e.getFieldError().getDefaultMessage();
        return message;//"请求参数格式错误";*/
        /*方法二*/
        StringJoiner stringJoiner = new StringJoiner("，","请求参数格式错误,","!!");
        List<FieldError> fieldErrors = e.getFieldErrors();
        for (FieldError f:fieldErrors
             ) {
            message = f.getDefaultMessage();
            stringJoiner.add(message);
        }

        return JsonResult.fail(ServiceCode.ERR_CONTACT,stringJoiner.toString());



    }

    @ExceptionHandler
    public JsonResult handlerThrowable(Throwable e){
        log.debug("程序中运行处出现的异常，将统一处理");
        log.warn("处理的异常信息，{}",e.getMessage());
        log.warn("异常：",e);//取代代码e.printStackTrace();效果相同但不会出现阻塞运行情况
        message = "服务器正忙，请稍后重试";//网络走丢了，请稍后
//        return message;
        return JsonResult.fail(ServiceCode.ERR_SYS,message);
    }

}
